package com.lei.springbootpractice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lei.springbootpractice.dao.AddressDao;
import com.lei.springbootpractice.pojo.AddressPojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressService {
    private static final Logger logger = LoggerFactory.getLogger(AddressService.class);
    @Autowired
    AddressDao addressDao;

    public List<AddressPojo> getAddressesByUserId(String userId) {
        logger.info("查询用户ID为 {} 的地址", userId);
        List<AddressPojo> addresses = addressDao.selectList(new QueryWrapper<AddressPojo>()
            .select("address_id", "user_id", "address", "username", "phone")
            .eq("user_id", userId));

        logger.info("找到 {} 个地址", addresses.size());
        for(AddressPojo address : addresses) {
            logger.debug("完整地址对象: {}", address);
            logger.debug("address_id: {}, user_id: {}", address.getAddressId(), address.getUserId());
        }
        return addresses;
    }

    @Transactional
    public AddressPojo addAddress(AddressPojo address) {
        try {
            logger.info("尝试添加地址: {}", address);
            logger.debug("地址详情 - ID: {}, 用户ID: {}, 地址内容: {}",
                address.getAddressId(), address.getUserId(), address.getAddress());
            int result = addressDao.insert(address);
            logger.info("添加地址结果: {}, 影响行数: {}", result > 0, result);
            if (result <= 0) {
                logger.error("添加地址失败，影响行数为0");
                return null;
            }
            logger.debug("插入后生成的address_id: {}", address.getAddressId());
            return address;
        } catch (Exception e) {
            logger.error("添加地址时发生异常", e);
            throw e;
        }
    }

    @Transactional
    public boolean updateAddress(AddressPojo address) {
        try {
            logger.info("尝试更新地址: {}", address.getAddressId());
            int result = addressDao.updateById(address);
            logger.info("更新地址结果: {}, 影响行数: {}", result > 0, result);
            return result > 0;
        } catch (Exception e) {
            logger.error("更新地址时发生异常", e);
            throw e;
        }
    }

    @Transactional
    public boolean deleteAddress(String addressId) {
        try {
            logger.info("尝试删除地址: {}", addressId);
            int result = addressDao.deleteById(addressId);
            logger.info("删除地址结果: {}, 影响行数: {}", result > 0, result);
            return result > 0;
        } catch (Exception e) {
            logger.error("删除地址时发生异常", e);
            throw e;
        }
    }

    public AddressPojo getAddressById(String addressId) {
        logger.info("查询地址ID: {}", addressId);
        if (addressId == null || addressId.trim().isEmpty()) {
            logger.warn("地址ID为空");
            return null;
        }
        AddressPojo address = addressDao.selectById(addressId);
        if (address == null) {
            logger.warn("未找到地址ID为 {} 的记录", addressId);
        } else {
            logger.debug("找到地址记录: {}", address);
        }
        return address;
    }
}
